<?php

namespace app\admin\model;

use app\lib\Qiniu;

use app\admin\model\Image;

class Messages extends Base
{

    public static function _after_delete($id){
        if(is_array($id)){
            $list = Image::where('relatedTable', 'messages')->whereIn('relatedId', $id)->select();
        }else{
            $list = Image::where('relatedTable', 'messages')->where('relatedId', $id)->select();
        }

        $Qiniu = new Qiniu();
        foreach ((array)$list as $v) {
            $key = substr(strrchr($v['imgUrl'], '/'), 1);
            // 删除七牛图片
            $res = $Qiniu->delImg($key);

            Image::where('id', $v['id'])->delete();
        }
    }


    /*// 图片关联表
    public function image() {
        return $this->hasMany('Image','relatedId');
    }*/

    // 关联本表，获取村委回复
    public function replay() {
        return $this->hasMany('Messages','responderId')->where('isDelete',2);
    }

    // 关联村民表
    public function member() {
        return $this->belongsTo('member','userId');
    }

} 